const users = require('../mongodb/users')
const mongodb = require('../mongodb/index')
const fs = require('fs')
const path = require('path')

module.exports = function (){
    return (req,res,next)=>{
        const obj = req.body
        obj.tel -= 0
        obj.hobby = obj.hobby.split(/,|\uff0c/)

        const oldName = path.join(__dirname,'..',req.files[0].path)
        const newName = oldName + '-' + req.files[0].originalname
        fs.renameSync(oldName,newName)
        const serverUrl = 'http://localhost:65535' + newName.split('\\user_upload')[1]
        console.log(newName.split('\\user_upload'))
        obj.avatar = serverUrl
        mongodb.set(users,{userid:obj.userid},obj,()=>{
            res.send('修改成功')
        })
    }
}